/**
 * Created by wenchuan.li 2018/5/23.
 */
define(function () {

    var self,
        app = win.app,
        user,
        pageParameters
    ;

    var reg = app.Module.extend({

        init: function () {
            self = this;
            pageParameters = app.pageParameters['register-modal'];
            user = pageParameters.item;
            user.GWAA009_NAME = user.GWAA009;
            user.ROLE_ID = '';
            self.data = {
                provinces: [],
                citys: [],
                industry: [],
                plants: [],
                customers: [],
                roles:[],
                sub: user
            };
            self._super({}, win);
        },
        subData: function () {
            var _data = self.data.sub;

            app.ajax({
                xurl: "api/wx/user/checkUserData",
                loading: true,
                xdata: _data,
                before: function () {
                    if(_data.ROLE_ID==''){
                        return '请选择所属角色';
                    }
                    if (_data.GWAA007 == '1') {

                        if (!_data.GWAA018) {
                            return '请选择城市';
                        }
                        if (!_data.GWAA009) {
                            return '请选择客户名称';
                        }
                    }
                },
                confirm: {
                    msg: '是否提交审核信息？'
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '审核成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                    } else {
                        app.alert({
                            msg: result.data.appmsg,
                            ok: function () {
                                if (result.data.appcode == 600) {
                                    app.back(1);
                                }
                            }
                        });
                    }
                    pageParameters.cb();
                }
            });
        },
        getProvince: function () {
            app.ajax({
                xurl: "api/wx/user/getProvinces",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var provinces = result.data.data;
                        if (!self.data.sub.GWAA017) {
                            self.data.sub.GWAA017 = provinces[0].GWAD002;
                        }
                        self.data.provinces.pushArray(provinces);
                        self.getCity();
                    }
                }
            });
        },
        getCity: function (ischange) {
            app.ajax({
                xurl: "api/wx/user/getCitys",
                xdata: {
                    GWAD003: self.data.sub.GWAA017
                },
                cb: function (result) {
                    if (result.ok) {
                        if (self.data.citys != 0) {
                            self.data.citys.removeAll();
                        }
                        var citys = result.data.data;
                        if (ischange) {
                            self.data.sub.GWAA018 = citys[0].GWAD002;
                        }
                        self.data.citys.pushArray(citys);
                    }
                }
            });
        },
        getIndustry: function () {
            app.ajax({
                xurl: "api/wx/user/getGwbwDatas",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var industry = result.data.data;
                        self.data.sub.industry = industry[0].GWBW001;
                        self.data.industry.pushArray(industry);
                    }
                }
            });
        },
        getCustomers: function (ischange) {
            app.ajax({
                xurl: "api/wx/user/getGwakDatas",
                xdata: {
                    GWAK024: self.data.sub.GWAA018,
                    GWAKL004: self.data.sub.GWAA009
                },
                cb: function (result) {
                    if (result.ok) {
                        if (self.data.customers.length != 0) {
                            self.data.customers.splice(0, self.data.customers.length)
                        }
                        var customers = result.data.data;
                        console.log(ischange)
                        if (ischange) {
                            console.log(ischange)
                            self.data.sub.GWAK001 = customers[0].GWAK001;
                        }

                        self.data.customers.pushArray(customers);
                    }
                }
            });
        },
        getPlant: function () {
            app.ajax({
                xurl: "api/wx/user/getPlantDatas",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var plants = result.data.data;
                        self.data.sub.GWAA008 = plants[0].ID;
                        self.data.plants.pushArray(plants);
                    }
                }
            });
        },
        _lazy: function () { // 延迟300ms加载

            self.getProvince();
            self.getIndustry();
            self.getCustomers();
            self.getPlant();

            app.ajax({
                xurl:"api/sys/getRoleList",
                xdata:{},
                cb:function (res) {
                    if (res.ok)
                        self.data.roles.pushArray(res.data.data);
                }
            })
            // app.psys.getRoleList({
            //     cb:function (data) {
            //
            //     }
            // });

            this._super();

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });

            app.taps.call(win, [{
                o: 'submitDiv',
                cb: function (e) {
                    self.subData();
                }
            }]);

        },
        _ready: function () { // dom载入完成后执行
            new Vue({
                el: app.getByid(doc, 'mui-scroll'),
                data: self.data,
                methods: {
                    provinceChange: function () {
                        self.getCity(true);
                        self.getCustomers(true);
                    },
                    customersChange: function () {
                        self.getCustomers(true);
                    }
                }
            });
        }
    });

    new reg();

});